package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Book;
import com.qianfeng.mapper.BookMapper;
import com.qianfeng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public Page getBookPage(int pageNo, int pageSize) {
        Page page = new Page();
        //PageHelper必须紧挨着，；拦截上面请求
        PageHelper.startPage(pageNo,pageSize);
        List<Book> bookList = bookMapper.getBookList();
        PageInfo<Book> bookPageInfo = new PageInfo<>(bookList);

        long total = bookPageInfo.getTotal();
        List<Book> data = bookPageInfo.getList();
        page.setCount(total);
        page.setData(data);
        page.setCode(200);
        page.setMsg("success");
        return page;
    }
    @Transactional
    @Override
    public List<Book> getBookList() {
        return bookMapper.getBookList();
    }

    @Transactional
    @Override
    public Book getBookBybookId(Integer bookId) {
        return bookMapper.getBookBybookId(bookId);
    }

    @Transactional
    @Override
    public void addBook(Book book) {
        bookMapper.addBook(book);
    }
    @Transactional
    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }
    @Transactional
    @Override
    public void deleteBook(Integer bookId) {
        bookMapper.deleteBook(bookId);
    }
}
